package com.example.administrator.wallet.view.activity.finds.kkbank;


import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.AssetListAdapter;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.MainBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.asset.RecordActivity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
//充值
public class DelegateRecordActivity extends BaseActivity {
    @Bind(R.id.finish_img)
    ImageButton bt_back;
    @Bind(R.id.view_title)
    TextView mV_title;
    @Bind(R.id.lv_DelegateRecordActivity)
    ListView lv_DelegateRecordActivity;

    IRequestManager reqequestManager = RequestFactory.getReqequestManager();

    private List<MainBean.DataBean.ListBean> dataBeanList;
    public AssetListAdapter adapter;//DelegateRecordAdapter
    private MainBean mainBean;



    @Override
    public void init() {
        bt_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.Delegate_record);

        setStatuColor();
        initData();


    }
    public void initAdapter() {
        adapter = new AssetListAdapter(DelegateRecordActivity.this, dataBeanList);
        lv_DelegateRecordActivity.setAdapter(adapter);
    }
    private void initData() {
//        iDatas = new ArrayList<>();
        dataBeanList = new ArrayList<>();
        String token = SharedPrefsUtil.getValue(DelegateRecordActivity.this, "wallet", "token", null);
        reqequestManager.httpPost(Api.BaseUrl + Api.Main_List, Api.getMainList(token, ""), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                Gson gson = new Gson();
                mainBean = gson.fromJson(Response, MainBean.class);
                if (mainBean.getCode() == 200) {
                    if (mainBean.getData().getList() != null) {
                        dataBeanList.addAll(mainBean.getData().getList());
                        initAdapter();
                        OnClick();
//                        smart.finishRefresh();
                    } else {
                        initData();
                    }
                }
            }

            @Override
            public void onFailure(String error) {
//                smart.finishRefresh();
//                toast(error);
            }
        });
    }
    private void OnClick() {
        adapter.getClickPst(new AssetListAdapter.OnClick() {
            @Override
            public void Click(MainBean.DataBean.ListBean dataBean) {
                Intent intent = new Intent(DelegateRecordActivity.this, RecordActivity.class);
                intent.putExtra("datas", dataBean);
                DelegateRecordActivity.this.startActivity(intent);
            }
        });

//        smart.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(RefreshLayout refreshlayout) {
//                if (refreshlayout.isRefreshing()) {
//                    initData();
//                }
//            }
//        });
//
//        smart.setOnLoadmoreListener(null);
//        smart.setLoadmoreFinished(true);
    }
    @Override
    public void onResume() {
        super.onResume();
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_delegate_record;
    }
}
